import { Entity, Column, PrimaryGeneratedColumn, AfterInsert, OneToMany, ManyToMany, JoinTable } from 'typeorm'
import { IsEmail, IsNotEmpty, Matches } from 'class-validator'
import { Base } from './Base'
import { Role } from './Role'
// import { IsUserAlreadyExist } from '../validate/IsUserAlreadyExist'

@Entity()
export class User extends Base {
  @Column({
    nullable: false,
    unique: true,
  })
  @IsNotEmpty()
  @Matches(/^[a-z0-9\-_]{5,20}$/i, {
    message: 'incorrect Login',
  })
  // @IsUserAlreadyExist({
  //   message: 'User login $value already exists. Choose another name.',
  // })
  public username!: string

  @Column({
    length: 100,
    unique: true,
    nullable: false,
  })
  @IsEmail()
  @IsNotEmpty()
  // @IsUserAlreadyExist({
  //   message: 'Email $value already exists.',
  // })
  public email!: string

  @IsNotEmpty()
  @Column({
    length: 80,
  })
  public firstName!: string

  @Column({
    length: 80,
    nullable: true,
  })
  public lastName!: string

  @Column({
    nullable: false,
    select: false,
  })
  @IsNotEmpty({
    message: 'Password must not be empty',
  })
  @Matches(/^[a-z0-9\-_!@#$%]{5,20}$/i, {
    message: 'incorrect Password',
  })
  public password!: string;

  @ManyToMany(() => Role, role => role.users)
  @JoinTable()
  public roles!: Role[];

  @AfterInsert()
  protected afterSave(): void {
    console.log('Create user: ', this.email)
  }
}